package org.kwisatz.proxybot.filters;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.kwisatz.proxybot.FilterAdapter;
import org.kwisatz.proxybot.Message;

public class PrivateMsg extends FilterAdapter
{
	private static final Pattern PATTERN_PV=Pattern.compile("^PRIVMSG (\\S+) :!pv");
	private static final Pattern PATTERN_NOPV=Pattern.compile("^PRIVMSG (\\S+) :!nopv");
	private static final Pattern PATTERN_MSG=Pattern.compile(":\\S+ (?:PRIVMSG|NOTICE) (\\S+) :.*");
	private boolean block;
	
	public String handleClientLine(String line, Message message)
	{
		if(PATTERN_PV.matcher(line).matches())
		{
			block=false;
			clientNotice.println("Private messages delivered normally");
			return null;
		}
		else if(PATTERN_NOPV.matcher(line).matches())
		{
			block=true;
			clientNotice.println("Private messages blocked");
			return null;
		}
		else
			return line;		
	}
	
	public String handleServerLine(String line, Message message)
	{
		if(block)
		{
			Matcher m = PATTERN_MSG.matcher(line);
			if(m.matches())
			{
				if(m.group(1).equals(context.getNickName()))
					return null;
			}
		}
		return line;
	}
}
